#!/usr/bin/python
# -*- coding: UTF-8 -*-

import tkinter as tk

from pyWinhook import HookManager

from utils import *


class App:
    def __init__(self, master):
        self.hm = None
        self.top_window = None
        self.session_hwd = None
        self.color = None
        self.mark = False  # 是否开始
        self.first_mark = False
        self.rect = (0, 0)  # 鼠标坐标
        master.bind('<Escape>', lambda e: root.destroy())
        frame = tk.Frame(master)
        frame.pack(side=tk.LEFT, padx=10, pady=10)
        self.open = tk.Button(frame, text='开始', fg='blue', command=self.start)
        self.open.pack()
        self.close = tk.Button(frame, text='结束', fg='blue', command=self.end)
        self.close.pack()
        self.get_rect = tk.Button(frame, text='坐标', fg='blue', command=self.show)
        self.get_rect.pack()

    # 开始催单
    def start(self):
        # 倒计时后开始
        while True:
            time.sleep(2)
            self.top_window = win32gui.GetForegroundWindow()
            self.session_hwd = get_session_hwd(self.top_window)
            is_curr_session(self.session_hwd)

    # 结束催单程序
    def end(self):
        pass

    # 点击'展示'开始获取鼠标所在的坐标.鼠标再次点击后的最后的坐标
    def show(self):
        if self.get_rect['text'] == '坐标':
            self.get_rect['text'] = '请移动到目标地点'
            self.hm = HookManager()
            self.hm.MouseAll = self.on_mouse
            self.hm.KeyAll = self.on_keyboard
            self.hm.HookMouse()
            self.hm.HookKeyboard()
        else:
            self.get_rect['text'] = '坐标'
            self.hm.UnhookMouse()
            self.hm.UnhookKeyboard()
        return True

    # 鼠标触发
    def on_mouse(self, event):
        if 'mouse middle down' == event.MessageName:
            if not self.mark:

                # 获取当前鼠标位置
                self.get_rect['text'] = '正在运行中'
                self.open['text'] = '正在运行中'
                self.mark = True
                self.rect = win32api.GetCursorPos()
                print(self.rect)
                while self.mark:
                    time.sleep(2)
                    if not self.first_mark:
                        self.top_window = win32gui.GetForegroundWindow()
                        self.session_hwd = get_session_hwd(self.top_window)
                        self.first_mark=True
                    if is_curr_session(self.session_hwd) == 'blue':  # 判断当前是当前会话
                        if curr_status(self.session_hwd) == 'yellow':  # 判断当前购物状态是否未付款
                            print("yellow for")
                            # 输入一次即可
                            once = False
                            while not once:
                                key_input()
                                hwd = get_text_hwd(self.top_window)
                                key_text_input(hwd, "您好亲")
                                once = True
                # 显示在窗口
            else:
                self.mark = False
                self.open['text'] = '开始'
                self.get_rect['text'] = '坐标'

        return True

    # 键盘触发
    def on_keyboard(self, event):
        if event.Key == 'F7':
            # 获取当前鼠标位置
            self.rect = win32api.GetCursorPos()
            # 显示在窗口
        return True


root = tk.Tk()
app = App(root)
# 主事件循环
root.mainloop()
